package it.polimi.traveldream.beans;

import it.polimi.traveldream.dto.AccomodationDTO;
import it.polimi.traveldream.dto.CartPackageDTO;
import it.polimi.traveldream.dto.ScheduleDTO;
import it.polimi.traveldream.dto.TransportDTO;
import it.polimi.traveldream.ejb.CustomerManager;
import it.polimi.traveldream.ejb.DefaultPackageManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.FlowEvent;
import org.primefaces.event.SelectEvent;

@ManagedBean(name = "cartPackageBean")
@ViewScoped
public class CartPackageBean {

	@EJB
	private CustomerManager manager;

	@EJB
	private DefaultPackageManager defaultPackageManager;

	private CartPackageDTO pack;

	public CartPackageBean() {
	}

	@PostConstruct
	public void init() {
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext()
				.getRequestParameterMap();
		String id = params.get("packageId");
		if (id != null && !id.isEmpty()) {
			this.pack = new CartPackageDTO(this.defaultPackageManager.findPackage(Integer
					.valueOf(id)));
		} else {
			this.pack = new CartPackageDTO();
		}
	}

	public CartPackageDTO getPack() {
		return this.pack;
	}

	public void setPack(CartPackageDTO pack) {
		this.pack = pack;
	}

	public String selectGoing(TransportDTO transport) {
		this.pack.setGoing(transport);
		return null;
	}

	public String selectBack(TransportDTO transport) {
		this.pack.setBack(transport);
		return null;
	}

	public String selectAccomodation(AccomodationDTO accomodation) {
		this.pack.setAccomodation(accomodation);
		return null;
	}

	public List<Integer> getDaysList() {
		List<Integer> list = new ArrayList<>();
		if (this.pack.getDuration() != null) {
			// la durata è espressa in notti, i giorni iniziano dallo 0 (giorno
			// dell' andata)
			// le attività sono pianificate nei giorni da 1 a n-1 (si escludono
			// il giorno di arrivo e partenza)
			for (int i = 1; i < this.pack.getDuration(); i++) {
				list.add(i);
			}
		}
		return list;
	}

	public void chooseActivity(Integer day) {
		Map<String, List<String>> param = new HashMap<>();

		List<String> list = new ArrayList<>();
		list.add(this.pack.getCity());
		param.put("city", list);

		list = new ArrayList<>();
		list.add(day.toString());
		param.put("day", list);

		list = new ArrayList<>();
		ScheduleDTO scheduleDTO = this.pack.getSchedule().get(day);
		if (scheduleDTO != null)
			list.add(scheduleDTO.getActivity().getId().toString());
		param.put("selected", list);

		RequestContext.getCurrentInstance().openDialog("/activitiesDialog", null, param);
	}

	public void selectActivity(SelectEvent event) {
		this.pack.addSchedule((ScheduleDTO) event.getObject());
	}

	public String flowListener(FlowEvent event) {
		return event.getNewStep();
	}

	public String getUrl() {
		String url = String.format(
				"/sharedPackage?model=%d&date=%d&duration=%d&going=%d&back=%d&accomodation=%d",
				this.pack.getModel().getId(), this.pack.getDate().getTime(),
				this.pack.getDuration(), this.pack.getGoing().getId(), this.pack.getBack().getId(),
				this.pack.getAccomodation().getId());
		for (ScheduleDTO sched : this.pack.getSchedule().values()) {
			url += "&schedule=" + sched.getDay() + "-" + sched.getActivity().getId();
		}
		return url;
	}

	public String addToCart() {
		this.manager.addToCart(this.pack);
		return "/customer/cart?faces-redirect=true";
	}

	public String addToGiftlist() {
		this.manager.addToGiftlist(this.pack);
		return "/giftList?faces-redirect=true";
	}
}
